﻿namespace Pibby.Core.Playlist
{
    using System;
    using System.IO;

    using Pibby.Core.IO;

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "u")]
	public static class M3uPlaylistWriter
	{
		public static void SaveTo(IFolder folder, string playlistName, IPlaylist playlist)
		{
			if (folder == null)
				throw new ArgumentNullException("folder", "Folder cannot be null");

			if (string.IsNullOrEmpty(playlistName))
				throw new ArgumentNullException("playlistName", "Play list name cannot be blank");

			if (playlist == null)
				throw new ArgumentNullException("playlist", "Playlist cannot be null");
		}
		
		public static void SaveTo(TextWriter writer, IPlaylist playlist, IFolder baseFolder)
		{
			const string m3uHeader = "#EXTM3U";
			const string extInfo = "#EXTINF:";

			if (writer == null)
				throw new ArgumentNullException("writer", Resources.Playlist_NullObjectError);

			if (playlist == null)
				throw new ArgumentNullException("playlist", Resources.Playlist_NullObjectError);

			writer.WriteLine(m3uHeader);

			foreach (PlaylistItem item in playlist.Items)
			{
				writer.WriteLine("{0}{1},{2}", extInfo, 0, Path.GetFileNameWithoutExtension(item.Path));
				writer.WriteLine(item.RelativePathTo(baseFolder.Path));
				writer.WriteLine();
			}
		}

	}
}
